import { Mock } from "mockjs";

// 模拟用户数据
const users = [
  {
    id: 1,
    username: "admin",
    password: "123456",
    name: "管理员",
    avatar: "https://avatars.githubusercontent.com/u/1?v=4",
    roles: ["admin"],
    token: "admin-token-123456",
    permissions: ["*"],
  },
  {
    id: 2,
    username: "yonghu1",
    password: "123456",
    name: "用户1",
    avatar: "https://avatars.githubusercontent.com/u/2?v=4",
    roles: ["yonghu1"],
    token: "yonghu1-token-123456",
    permissions: ["demo:read", "demo:write"],
  },
  {
    id: 3,
    username: "yonghu2",
    password: "123456",
    name: "用户2",
    avatar: "https://avatars.githubusercontent.com/u/3?v=4",
    roles: ["yonghu2"],
    token: "yonghu2-token-123456",
    permissions: ["demo:read"],
  },
];

export default [
  // 用户登录接口
  {
    url: "/api/login",
    method: "post",
    response: ({ body }) => {
      const { username, password } = body;
      const user = users.find(
        (u) => u.username === username && u.password === password
      );

      if (user) {
        return {
          code: 1,
          message: "登录成功",
          data: {
            userInfo: {
              id: user.id,
              username: user.username,
              name: user.name,
              avatar: user.avatar,
              roles: user.roles,
              token: user.token,
              permissions: user.permissions,
            },
          },
        };
      } else {
        return {
          code: 0,
          message: "用户名或密码错误",
          data: null,
        };
      }
    },
  },

  // 获取用户信息接口
  {
    url: "/api/userInfo",
    method: "get",
    response: ({ headers }) => {
      const token = headers.token;
      const user = users.find((u) => u.token === token);

      if (user) {
        return {
          code: 1,
          message: "获取用户信息成功",
          data: {
            userInfo: {
              id: user.id,
              username: user.username,
              name: user.name,
              avatar: user.avatar,
              roles: user.roles,
              token: user.token,
              permissions: user.permissions,
            },
          },
        };
      } else {
        return {
          code: 0,
          message: "token无效",
          data: null,
        };
      }
    },
  },

  // 用户列表接口
  {
    url: "/api/users",
    method: "get",
    response: () => {
      return {
        code: 1,
        message: "获取用户列表成功",
        data: {
          list: users.map((user) => ({
            id: user.id,
            username: user.username,
            name: user.name,
            avatar: user.avatar,
            roles: user.roles,
            permissions: user.permissions,
          })),
          total: users.length,
        },
      };
    },
  },

  // 添加用户接口
  {
    url: "/api/users",
    method: "post",
    response: ({ body }) => {
      const newUser = {
        id: users.length + 1,
        ...body,
        token:
          Mock.mock("@string('lower', 20)") +
          "-token-" +
          Mock.mock("@string('number', 6)"),
      };
      users.push(newUser);

      return {
        code: 1,
        message: "添加用户成功",
        data: {
          userInfo: newUser,
        },
      };
    },
  },

  // 更新用户接口
  {
    url: "/api/users/:id",
    method: "put",
    response: ({ body, query }) => {
      const userId = parseInt(query.id);
      const userIndex = users.findIndex((u) => u.id === userId);

      if (userIndex !== -1) {
        users[userIndex] = { ...users[userIndex], ...body };
        return {
          code: 1,
          message: "更新用户成功",
          data: {
            userInfo: users[userIndex],
          },
        };
      } else {
        return {
          code: 0,
          message: "用户不存在",
          data: null,
        };
      }
    },
  },

  // 删除用户接口
  {
    url: "/api/users/:id",
    method: "delete",
    response: ({ query }) => {
      const userId = parseInt(query.id);
      const userIndex = users.findIndex((u) => u.id === userId);

      if (userIndex !== -1) {
        users.splice(userIndex, 1);
        return {
          code: 1,
          message: "删除用户成功",
          data: null,
        };
      } else {
        return {
          code: 0,
          message: "用户不存在",
          data: null,
        };
      }
    },
  },
];
